package com.zj.framework.httpclient;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xi.yang(xi.yang@downjoy.com)
 * @create 2018-05-29 下午 3:24
 **/
public class ZyltMessageSend {
    private static final int CONNECTION_TIMEOUT = 5 * 1000;

    private static final int REQUEST_TIMEOUT = 10 * 1000;
    private static final String CHAR_SET = "gbk";
    public static void main(String[] args) throws Exception {
        long touid = 49300382L;
        String message = "老王，再收不到就没法了";
        String url = String.format("http://bbs.zhiyoo.com/home.php?mod=spacecp&ac=pm&op=showmsg&handlekey=showmsg_2537733&touid=%s&pmid=0&daterange=2", touid);
        System.out.println(url);
        DefaultHttpClient httpclient =new DefaultHttpClient();
        List<NameValuePair> nvpsList = new ArrayList<>();
        String connectUrl = HttpUtil.buildGetUrl(url, nvpsList);
        HttpGet httpGet = new HttpGet(connectUrl);
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(REQUEST_TIMEOUT).setConnectTimeout(CONNECTION_TIMEOUT).build();
        httpGet.setConfig(requestConfig);
        HttpResponse response = httpclient.execute(httpGet);

        System.out.println(response.toString());

        HttpPost httpPost = new HttpPost("http://bbs.zhiyoo.com/home.php?mod=spacecp&ac=pm&op=send&pmid=1147481&daterange=0&handlekey=pmsend&pmsubmit=yes&inajax=1");
        String formData = String.format("message=%s&formhash=e89e3485", message);
        StringEntity s = new StringEntity(formData,CHAR_SET);
        s.setContentType("application/x-www-form-urlencoded");
        s.setContentEncoding(CHAR_SET);
        httpPost.setEntity(s);
        DefaultHttpClient httpclient2 = new DefaultHttpClient();
        httpclient2.setCookieStore(httpclient.getCookieStore());
        response = httpclient2.execute(httpPost);
        System.out.println("========结果=======");
        System.out.println(response.toString());
    }

}

